<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/19
 * Time: 18:02
 */

// 首先开启session
// 把username存在$_SESSION['user'] 里面
// 直接输出 username

include "../conn.php";


//include 'conn.php';
//
//
////分组算法
//
//$groupAd;
//$tArr;
//$sArr;
//$groupNum;
//$sNum;
//$tNum;
//
//
//$sArr=range(1,75);//学生人数
//$tArr=range(1,11);//教师人数
//
//$sGroupNum=floor(count($sArr)/10);//学生分组(组数)
//$sOut=count($sArr)%10;//多出个数
//
//$tGroupNum=floor(count($tArr)/3);//教师分组(组数)
//$tOut=count($tArr)%3;//多出个数
//
//$sGroups=array_chunk($sArr,10,false);
//$tGroups=array_chunk($tArr,3,false);
//
//
//if($sOut!=0){
//   $outArr=$sGroups[$sGroupNum];
//    $j=0;
//    for($i=0;$i<$sOut;$i++)
//    {
//        if($j>$sGroupNum-1){
//            $j=0;
//        }
//        array_push($sGroups[$j],$outArr[$i]);
//        $j++;
//    }
//    unset($sGroups[$sGroupNum]);
//}
//
//
//echo $sGroupNum."组学生";
//echo "<pre>";
//print_r($sGroups);
//echo "<pre>";
//
//
//if($tOut!=3){
//    $outArr=$tGroups[$tGroupNum];
//    $j=0;
//    for($i=0;$i<$tOut;$i++)
//    {
//        if($j>$tGroupNum-1){
//            $j=0;
//        }
//        array_push($tGroups[$j],$outArr[$i]);
//        $j++;
//    }
//    unset($tGroups[$tGroupNum]);
//}
//
//
//echo $tGroupNum."组教师";
//echo "<pre>";
//print_r($tGroups);
//echo "<pre>";
//
//$j=0;
//for($i=0;$i<$sGroupNum;$i++)
//{
//    if($j>$tGroupNum-1)
//    {
//        $j=0;
//    }
//    $defenGroup[$i]=array('t'=>$tGroups[$j],'s'=>$sGroups[$i]);
//    $j++;
//}
//
//echo "最终答辩分组：".count($defenGroup)."组";
//echo "<pre>";
//print_r($defenGroup);
//echo "<pre>";


//$sql1="select TeacherName from teacher where roomId='2'";
//$sql2="select StudentName from student WHERE Major='网络传播'";
//$result1=mysqli_query($conn, $sql1);
//$row1=$result1->fetch_assoc();
//$result2=mysqli_query($conn, $sql2);
//$row2=$result2->fetch_all();
//$defenGroup=array('t'=>$row1,'s'=>$row2);
//echo "<pre>";
//echo $row1["TeacherName"];
//echo "<pre>";


//
//$tarr=$_POST["t"];
//echo count($tarr);
//echo "<pre>";
//print_r($_POST["t"]);
//echo "<pre>";
//
//$tarr=$_POST["t"]?$_POST["t"]:null;
//$groupId="2001";
//$defenPoint="d102";
//$groupAd='000';
//$defenTime="ddddd";
//
//
//
//for($i=0;$i<count($tarr);$i++)
//{
//    $sql="update teacher set groupid='$groupId' WHERE teacherId='$tarr[$i]'";
//    if(mysqli_query($conn, $sql))
//    {
//        echo "yes";
//    }else{
//        echo "Error: " . $sql . "<br>" . mysqli_error($conn);
//    }
//}
//
//
//$tarr=$_POST["t"];
//echo count($tarr);
//echo "<pre>";
//print_r($_POST["t"]);
//echo "<pre>";

//论文评分、答辩评分任务分配，关键是否有关系


//for($i=0;$i<count($sArr);$i++)
//{
//        if($tArr[0]!=$sArr[$i]){
//            $sArr[$i];
//        }else
//        {
//
//        }
//
//}


//计算有多少个答辩组
//$sql="select groupId from defenseteam";
//$row=mysqli_query($conn,$sql)->fetch_all();
//echo count($row);
//
//    if(count($row)>0){
//    for($k=0;$k<count($row);$k++) {
//        $groupId=$row[$k][0];
//        $sql = "select studentId ,teacher_TeacherId from student where groupid=$groupId";//查找学生集
//        $sRow = mysqli_query($conn, $sql)->fetch_all();
//
//        echo "<pre>";
//        print_r($sRow);//学生集
//        echo "<pre>";
//
//        $sql = "select teacherId from teacher where teacherId in
//        (select teacherId FROM teacherasigntable where groupId=$groupId)";//查找教师集
//        $tRow = mysqli_query($conn, $sql)->fetch_all();
//
//        echo "<pre>";
//        print_r($tRow);//教师集
//        echo "<pre>";
//
//        //分配操作
//        for ($i = 0; $i < count($sRow); $i += count($tRow)) {
//            for ($j = 0; $j < count($tRow); $j++) {
//                if (!isset($sRow[$i + $j][1])) {break;}
//                $sId=$sRow[$i + $j][0];//学生ID
//                if ($sRow[$i + $j][1] == $tRow[$j][0]) {
//                    //不能分配任务/(分配给下一个老师)
//
//                    if ($j < count($tRow) - 1) {
//                        echo $tRow[$j + 1][0] . "<br>";
//                        $sRow[$i + $j][2] = $tRow[$j + 1][0];
//                        $Tid=$tRow[$j + 1][0];//评分教师ID
//                        $sql1="update thesismark set Teacher_teacherId=$Tid
//                            where project_projectid=(select projectId from project WHERE
//                             student_studentId=$sId) ";
//
//
//                    } else {
//                        echo $tRow[0][0] . "<br>";
//                        $sRow[$i + $j][2] = $tRow[0][0];
//                        $Tid=$tRow[0][0];//评分教师ID
//                        $sql1="update thesismark set Teacher_teacherId=$Tid
//                            where project_projectid=(select projectId from project WHERE
//                             student_studentId=$sId) ";
//                    }
//                }
//                else {
//                    //能分配任务/
//                    echo $tRow[$j][0] . "<br>";
//                    $sRow[$i + $j][2] = $tRow[$j][0];
//                    $Tid=$tRow[$j][0];//评分教师ID
//                    $sql1="update thesismark set Teacher_teacherId=$Tid
//                            where project_projectid=(select projectId from project WHERE
//                             student_studentId=$sId) ";
//                }
//                if(mysqli_query($conn,$sql1)){
//                        echo $sql1."<br>";
//                }else{ echo mysqli_error($conn);}
//            }
//        }
//        echo "<pre>";
//        print_r($sRow);
//        echo "<pre>";
//
//    }
//    }
//    else{
//        //未分组，要清除评分关系
//        $sql="update thesismark set Teacher_teacherId=null
//            where 1";
//        mysqli_query($conn,$sql);
//    }


//测试阶段先清除
$sql = "delete from thesismark where 1";
mysqli_query($conn, $sql);


$sql = "select groupId from defenseteam";
$row = mysqli_query($conn, $sql)->fetch_all();
echo count($row);

if (count($row) > 0) {
    for ($k = 0; $k < count($row); $k++) {
        $groupId = $row[$k][0];
        $sql = "select studentId ,teacher_TeacherId from student where groupid=$groupId";//查找学生集
        $sRow = mysqli_query($conn, $sql)->fetch_all();

        echo "<pre>";
        print_r($sRow);//学生集
        echo "<pre>";

        $sql = "select teacherId from teacher where teacherId in
        (select teacherId FROM teacherasigntable where groupId=$groupId)";//查找教师集
        $tRow = mysqli_query($conn, $sql)->fetch_all();

        echo "<pre>";
        print_r($tRow);//教师集
        echo "<pre>";

        //分配操作
        for ($i = 0; $i < count($sRow); $i += count($tRow)) {
            for ($j = 0; $j < count($tRow); $j++) {
                if (!isset($sRow[$i + $j][1])) {
                    break;
                }
                $sId = $sRow[$i + $j][0];//学生ID
                if ($sRow[$i + $j][1] == $tRow[$j][0]) {
                    //不能分配任务/(分配给下一个老师)

                    if ($j < count($tRow) - 1) {
                        echo $tRow[$j + 1][0] . "<br>";
                        $sRow[$i + $j][2] = $tRow[$j + 1][0];
                        $Tid = $tRow[$j + 1][0];//评分教师ID


                        $result = mysqli_query($conn, "select projectId from project WHERE student_studentId=$sId");
                        $Prow = $result->fetch_array();
                        $Pid = $Prow[0];
                        //插入
                        $sql1 = "insert into thesismark VALUES (
                        $Pid,'2018',null,null,null,'$Pid','$Tid'
                  )";


                    } else {
                        echo $tRow[0][0] . "<br>";
                        $sRow[$i + $j][2] = $tRow[0][0];
                        $Tid = $tRow[0][0];//评分教师ID

                        $result = mysqli_query($conn, "select projectId from project WHERE student_studentId=$sId");
                        $Prow = $result->fetch_array();
                        $Pid = $Prow[0];
                        //插入
                        $sql1 = "insert into thesismark VALUES (
                        $Pid,'2018',null,null,null,'$Pid','$Tid'
                  )";
                    }
                } else {
                    //能分配任务/
                    echo $tRow[$j][0] . "<br>";
                    $sRow[$i + $j][2] = $tRow[$j][0];
                    $Tid = $tRow[$j][0];//评分教师ID


                    $result = mysqli_query($conn, "select projectId from project WHERE student_studentId=$sId");
                    $Prow = $result->fetch_array();
                    $Pid = $Prow[0];
                    //插入
                    $sql1 = "insert into thesismark VALUES (
                        $Pid,'2018',null,null,null,'$Pid','$Tid'
                  )";

                }
                if (mysqli_query($conn, $sql1)) {
                    echo $sql1 . "<br>";
                } else {
                    echo $sql1 . mysqli_error($conn) . "<br>";
                }
            }
        }
        echo "<pre>";
        print_r($sRow);
        echo "<pre>";

    }
} else {
    //未分组，要清除评分关系
    $sql = "update thesismark set Teacher_teacherId=null
            where 1";
    mysqli_query($conn, $sql);
}


//课题添加业务逻辑（需要插入三张评分表)
//$sql="select projectId  from project ";
//$result=mysqli_query($conn,$sql);
//$rows=$result->fetch_all();
//echo count($rows)."个课题";
//
//$sql="delete from guidemark where 1";
//mysqli_query($conn,$sql);
//$sql="delete from thesismark where 1";
//mysqli_query($conn,$sql);
//$sql="delete from replymark where 1";
//mysqli_query($conn,$sql);
//
//
//for($i=0;$i<count($rows);$i++)
//{   $a=$rows[$i][0];
//    $sql="insert into guidemark VALUES (
//    $a,'2018',null,null,null,null,null,null,$a)";
//    mysqli_query($conn,$sql);
//    $sql="insert into thesismark VALUES (
//        $a,'2018',null,null,null,$a,null)";
//    mysqli_query($conn,$sql);
//    $sql="insert into replymark VALUES (
//        $a,'2018',null,null,null,null,null,$a,null)";
//    mysqli_query($conn,$sql);
//
//
//}


//选题操作（修改学生表和课题表）
//$sql="select projectId,Teacher_teacherId from project";
//$result=mysqli_query($conn,$sql);
//$Prows=$result->fetch_all();
//
//$sql1="select studentId from student";
//$result=mysqli_query($conn,$sql1);
//$Srows=$result->fetch_all();
//for($i=0;$i<count($Prows);$i++)
//{   $Pid=$Prows[$i][0];
//    $sId=$Srows[$i][0];
//    $Tid=$Prows[$i][1];
//    $sql="update project set student_studentId=$sId WHERE
//          projectId=$Pid";
//    mysqli_query($conn,$sql);
//    $sql="update student set teacher_teacherId=$Tid WHERE
//      studentId=$sId";
//    mysqli_query($conn,$sql);
//}


//$sql="select studentName from student s join
//project p on s.studentId=p.student_studentId
//join thesismark t on p.projectId=t.project_projectId
//where s.groupId='2001'and t.teacher_teacherId=1002";
//$result=mysqli_query($conn,$sql);
//$row=$result->fetch_all();
//echo "<pre>";
//print_r($row);
//echo "</pre>"


?>






